// 1. 声明reducer函数, 设置初始值和类型, 合并reducer函数

const initState = {
  suggests: [] as string[],
};
// 2. 定义ActionType
type ActionType = { type: 'search/saveSuggests'; payload: string[] };

function searchReducer(
  state = initState,
  action: ActionType
): typeof initState {
  switch (action.type) {
    case 'search/saveSuggests':
      // 4. 完成保存的计算
      return { ...state, suggests: action.payload.filter((item) => !!item) };
    default:
      return state;
  }
}

export default searchReducer;
